package com.jiedeng.controller.index;

import com.jiedeng.controller.Rst;
import com.jiedeng.dao.IPersonRepository;
import com.jiedeng.model.Person;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 初始登录页
 *
 * @author zhouyb
 */
@RestController
@RequestMapping("/init")
public class InitController {
    private final Logger log = LoggerFactory.getLogger(InitController.class);
    @Autowired
    IPersonRepository iPersonRepository;

    /**
     * 修改密码
     *
     * @param phoneNum 手机号码
     * @param password 密码
     * @return rst
     */
    @RequestMapping("resetPassword")
    public Rst resetPassword(String phoneNum, String password) {
        Rst rst = new Rst();
        if (StringUtils.isEmpty(phoneNum)) {
            rst.setResultCode(1);
            rst.setMsg("手机号为空");
        } else if (StringUtils.isEmpty(password)) {
            rst.setResultCode(1);
            rst.setMsg("密码为空");
        } else {
            List<Person> personList = iPersonRepository.findByPhone(phoneNum);
            if (personList != null && personList.size() > 0) {
                Person person = personList.get(0);
                person.setPassword(password);
                iPersonRepository.save(person);
                rst.setResultCode(0);
                rst.setMsg("修改成功");
            } else {
                rst.setResultCode(1);
                rst.setMsg("对象不存在");
            }
        }

        return rst;
    }
}